﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace AnaViewer
{
    public partial class AnaListPanel : UserControl
    {
        public AnaListPanel()
        {
            InitializeComponent();

            anaList.SelectedIndexChanged += new EventHandler(anaList_SelectedIndexChanged);
        }

        void anaList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (mySelf)//不是用户点击操作的
                return;
            mySelf = true;
            if (anaList.SelectedIndex > 0)//因为 0 为标题
            {
                //anaForm.SwitchMode = SwitchMode.Immediately;
                subject.SelectedIndex = anaList.SelectedIndex - 1;
                //anaForm.SwitchMode = SwitchMode.FadeInFadeOut;
            }
            mySelf = false;
        }


        AnaSubject subject;
        public AnaSubject Subject
        {
            get { return subject; }
            set
            {
                subject = value;
                if (subject != null)
                {
                    subject.SelectedIndexChanged += new EventHandler(subject_SelectedIndexChanged);
                    subject.ItemsListChanged += delegate(object sender, EventArgs e) { UpdateList(); };
                }
            }
        }

        /// <summary>
        /// 是否程序内部切换列表。
        /// </summary>
        bool mySelf = false;
        void subject_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (mySelf)
            //    return;

            mySelf = true;
            if (anaList.SelectedIndex != subject.SelectedIndex + 1)
                anaList.SelectedIndex = subject.SelectedIndex + 1;

            mySelf = false;

        }



        void UpdateList()
        {
            Cursor = Cursors.WaitCursor;

            anaList.Clear();
            IList<IItem> items = subject.AnaItems;
            foreach (IItem item in items)
            {
                string author = (item.Author == null || item.Author.Trim() == "") ? "佚名" : item.Author;
                string title = (item.Title == null || item.Title.Trim() == "") ? "(无标题)" : item.Title;
                string desc = (item.Description == null || item.Description.Trim() == "") ? "(无描述)" : item.Description;
                anaList.Items.Add(new RowItem(false, title, desc, item.Created, author));
            }

            if (items.Count > 0)
            {//如果原来为空，则显示
                subject.SelectedIndex = 0;
            }

            Cursor = Cursors.Default;
        }
    }
}
